package com.mec.web.y2012.coreservlet;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.log4j.Logger;

public class DBUtilities {

	public static String JDBC_DRIVER = "jdbcDriver";
	public static String DB_URL = "dbUrl";
	public static String USER_NAME = "userName"	;
	public static String PASSWORD = "password";
	//public static String CONFIG_FILE_DEFAULT = "./WEB-INF/classes/jdbc-config-default.properties";
	// 
	public static String CONFIG_FILE = "./conf/jdbc-config.properties";
	
	//
	//private String jdbcDriver = ""	;
	//private String url="";
	//private String userName = "";
	//private String password = "";
	private Properties configProperties = new Properties();
	
	
	//
	private Connection conn;
	private static final DBUtilities instance = new DBUtilities();
	
	private DBUtilities(){
		
	}
	
	public void setConnectionInfo(
			String jdbcDriver,
			String dbUrl,
			String userName,
			String passWord
			){
		configProperties.put(JDBC_DRIVER, jdbcDriver);
		configProperties.put(DB_URL, dbUrl);
		configProperties.put(USER_NAME, userName);
		configProperties.put(PASSWORD, passWord);
		
		//
		try {
			Class.forName(configProperties.getProperty(JDBC_DRIVER));
		} catch (Exception e) {
			logger.error(e);
		}
	}
	
	public synchronized Connection getConnection() throws SQLException{
		if(null == conn){
			try {
				conn = DriverManager.getConnection(
						configProperties.getProperty(DB_URL),
						configProperties.getProperty(USER_NAME),
						configProperties.getProperty(PASSWORD));
			} catch (SQLException e) {
				logger.error("SQLException occured when try to get connection", e);
				conn = null;
			}
		}
		
		//
		return conn;
	}
	
	
	/*static {
		logger = Logger.getLogger(DBUtilities.class);
		instance = new DBUtilities();
	}
	
	public DBUtilities(){
		try{
			applyConfig(CONFIG_FILE);
		}catch(Exception e){
			logger.error(e);
			//e.printStackTrace();
		}
	}
	
	private void initDatabaseParams(String configFile) throws FileNotFoundException, IOException{
		
		//
		configProperties = new Properties();
		
		//relative to /WEB-INF/
		URL webInfPath = getClass().getResource("/");// + configFile);
		String str = webInfPath.getFile();
		logger.info("str -> " + str);
		File file = new File(webInfPath.getPath());
		if(! file.exists()){
			logger.info(file.getAbsolutePath() + " not found.");
			throw new FileNotFoundException( configFile + " not found.");
		}
		
		
		FileReader reader = new FileReader(file);
		configProperties.load(reader);
	}
	
	public void applyConfig(String configFile){
		try{
			initDatabaseParams(configFile);
			String jdbcDriver = configProperties.getProperty(JDBC_DRIVER);
			if(null != jdbcDriver){
				Class.forName(configProperties.getProperty(JDBC_DRIVER));
			}else{
				throw new Exception("jdbcDriver is null.");
			}
		}catch(FileNotFoundException fnfe){
			logger.error(configFile + " not Found");
		}catch(Exception e){
			logger.error("Apply Config-File " + configFile + " Failed.", e);
		}
	}
	*/

	/*@Test
	public void testPath(){
		File file = new File("/");
		logger.info("path1 -> [/] " + file.getAbsolutePath());
		
		//
		File file2 = new File("./");
		logger.info("path2 -> [./] " + file2.getAbsolutePath());
		
		//
		logger.info("path3 -> [class.resource[/]] " + getClass().getResource("/").getPath());
		logger.info("path4 -> [class.resource[./]] " + getClass().getResource("./").getPath());
	}*/
	
	public static DBUtilities getInstance(){
		return instance;
	}
	
	private static final Logger logger = Logger.getLogger(DBUtilities.class);
}
